package org.jeecg;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import org.jeecg.modules.thirdpartyapi.constant.BgApiEnum;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

public class MppUtil {
    public static void main(String[] args) {
        String file = "H:\\java\\zhangshuiyi\\fk_back\\jeecg-boot-module-system\\src\\main\\java\\org\\jeecg";
        String old = "IService<";
        String newW = "IMppService<";
        String oldImport = "com.baomidou.mybatisplus.extension.service.IService";
        String newImport = "com.github.jeffreyning.mybatisplus.service.IMppService";
        ArrayList<String> list = new ArrayList<>();
        getAllFileName(file, list);
        BgApiEnum[] values = BgApiEnum.values();
        for (BgApiEnum bgApiEnum : values) {
            String entityName = StrUtil.toCamelCase(bgApiEnum.getValue());
            String mapper = entityName.substring(0, 1).toUpperCase() + entityName.substring(1, entityName.length()) + "Service.java";
            for (String fileList : list) {
                if (fileList.contains(mapper)) {

                    Path path = FileUtil.file(fileList).toPath();
                    List<String> fileContent = null;
                    try {
                        fileContent = new ArrayList<>(Files.readAllLines(path, StandardCharsets.UTF_8));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    Boolean aBoolean = replaceStr(fileContent, old, newW);
                    replaceStr(fileContent, oldImport, newImport);
                    try {
                        Files.write(path, fileContent, StandardCharsets.UTF_8);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    if (aBoolean){
                        System.out.println(fileList + "完成");
                    }else {
                        System.out.println(fileList + "不需要");
                    }

                }

            }

        }

    }

    //替换
    public static Boolean replaceStr(List<String> fileContent, String old, String newW) {
        for (int i = 0; i < fileContent.size(); i++) {
            if (fileContent.get(i).contains(newW)) {
                return false;
            }
            if (fileContent.get(i).contains(old)) {
                String replace = fileContent.get(i).replace(old, newW);
                fileContent.set(i, replace);
                break;
            }
        }
        return true;
    }


    //获取所有文件
    public static void getAllFileName(String path, ArrayList<String> list) {
        File file = new File(path);
        //这些路径名表示此抽象路径名所表示目录中的文件。
        File[] files = file.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                return true;
            }
        });
        for (int i = 0; i < files.length; i++) {
            //判断是否是目录，是的话继续递归
            if (files[i].isDirectory()) {
                getAllFileName(files[i].getAbsolutePath(), list);
            } else {
                list.add(files[i].getAbsolutePath());
            }
        }
    }

}
